package com.bd.demo.ui.view

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.bd.demo.ui.theme.lightSecondary
import com.bd.demo.ui.theme.text18BoldTitle
import com.google.accompanist.insets.statusBarsHeight
import com.bd.demo.R

@Preview
@Composable
fun TitleBar_Test() {
    TitleBar("测试", onClickBack = {

    })
}

/**
 * 标题栏
 * @param value String
 * @param onClickBack Function0<Unit>
 * @param content [@androidx.compose.runtime.Composable] [@kotlin.ExtensionFunctionType] Function1<BoxScope, Unit>
 */
@Composable
fun TitleBar(
    value: String,
    onClickBack: (() -> Unit)? = null,
    content: @Composable BoxScope.() -> Unit = {}
) {
    ElevationBox {
        Column {
            // 状态栏
            Spacer(modifier = Modifier
                .fillMaxWidth()
                .statusBarsHeight()
            )
            // 标题栏
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(),
                contentAlignment = Alignment.CenterStart
            ) {
                if (onClickBack != null) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_baseline_arrow_back_24),
                        contentDescription = "返回",
                        tint = lightSecondary,
                        modifier = Modifier
                            .size(22.dp)
                            .clickable { onClickBack.invoke() }
                    )
                }
                Text(
                    text = value,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(0.dp, 6.dp),
                    textAlign = TextAlign.Center,
                    style = text18BoldTitle
                )
            }
            // 标题栏下方内容
            Box (
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(),
                content = content
            )
        }
    }
}